import { createRouter, createWebHashHistory } from "vue-router";
import {Storage} from "@/utils/Storage.ts";

const constantRoutes = [
    { path: "/", component: () => import("@/components/structure/Login.vue") },
    { path: "/login", component: () => import("@/components/structure/Login.vue") },
    { path: '/home',
      component: () => import("@/components/structure/Home.vue"),
      redirect: '/home/index',meta:{auth:true}, children: [
         { path: '/home/index',meta:{auth:true}, component: () => import("@/components/modules/Index.vue")},
         { path: '/home/config',meta:{auth:true,theme:'配置信息设置'}, component: () => import("@/components/modules/Config.vue")},
         { path: '/home/:name',meta:{auth:true}, redirect: '/content/:name/search', children: [
             { path: '/home/system/search',meta: {auth:true,theme:'系统信息列表',page: ['/home/base']}, component: () => import("@/components/modules/SystemSearchPage.vue")},
             { path: '/home/system/edit',meta: {auth:true,theme:'系统信息编辑',page: ['/home/base','/home/system/search']}, component: () => import("@/components/modules/SystemEditPage.vue")},
             { path: '/home/login/search',meta: {auth:true,theme:'账号信息列表',page: ['/home/base']}, component: () => import("@/components/modules/LoginSearchPage.vue")},
             { path: '/home/login/edit',meta: {auth:true,theme:'账号信息编辑',page: ['/home/base','/home/login/search']}, component: () => import("@/components/modules/LoginEditPage.vue")},
             { path: '/home/login/relation',meta: {auth:true,theme:'账号关系绑定',page: ['/home/base','/home/login/search']}, component: () => import("@/components/modules/LoginRelationPage.vue")},
             { path: '/home/role/search',meta: {auth:true,theme:'角色信息列表',page: ['/home/base']}, component: () => import("@/components/modules/RoleSearchPage.vue")},
             { path: '/home/role/edit',meta: {auth:true,theme:'角色信息编辑',page: ['/home/base','/home/role/search']}, component: () => import("@/components/modules/RoleEditPage.vue")},
             { path: '/home/role/relation',meta: {auth:true,theme:'角色关系绑定',page: ['/home/base','/home/role/search']}, component: () => import("@/components/modules/RoleRelationPage.vue")},
             { path: '/home/permission/search',meta: {auth:true,theme:'权限信息列表',page: ['/home/base']}, component: () => import("@/components/modules/PermissionSearchPage.vue")},
             { path: '/home/permission/edit',meta: {auth:true,theme:'权限信息编辑',page: ['/home/base','/home/permission/search']}, component: () => import("@/components/modules/PermissionEditPage.vue")},
             { path: '/home/permission/relation',meta: {auth:true,theme:'权限关系绑定',page: ['/home/base','/home/permission/search']}, component: () => import("@/components/modules/PermissionRelationPage.vue")},
             { path: '/home/menu/search',meta: {auth:true,theme:'菜单信息列表',page: ['/home/base']}, component: () => import("@/components/modules/MenuSearchPage.vue")},
             { path: '/home/menu/edit',meta: {auth:true,theme:'菜单信息编辑',page: ['/home/base','/home/menu/search']}, component: () => import("@/components/modules/MenuEditPage.vue")},
             { path: '/home/menu/relation',meta: {auth:true,theme:'菜单关系绑定',page: ['/home/base','/home/menu/search']}, component: () => import("@/components/modules/MenuRelationPage.vue")},
             { path: '/home/router/search',meta: {auth:true,theme:'路由信息列表',page: ['/home/base']}, component: () => import("@/components/modules/RouterSearchPage.vue")},
             { path: '/home/router/edit',meta: {auth:true,theme:'路由信息编辑',page: ['/home/base','/home/router/search']}, component: () => import("@/components/modules/RouterEditPage.vue")},
             { path: '/home/router/relation',meta: {auth:true,theme:'路由关系绑定',page: ['/home/base','/home/router/search']}, component: () => import("@/components/modules/RouterRelationPage.vue")},
             { path: '/home/form/search',meta: {auth:true,theme:'表单信息列表',page: ['/home/base']}, component: () => import("@/components/modules/FormSearchPage.vue")},
             { path: '/home/form/edit',meta: {auth:true,theme:'表单信息编辑',page: ['/home/base','/home/form/search']}, component: () => import("@/components/modules/FormEditPage.vue")},
             ]
         },
         { path: '/home/settings',meta:{auth:true,theme:'基础信息设置',page: ['/home/settings']}, component: () => import("@/components/modules/Settings.vue")},
         { path: '/home/account',meta:{auth:true}, component: () => import("@/components/modules/Account.vue")},
         { path: '/home/demo1',meta:{auth:true,theme:'表单信息编辑',page: ['/home/demo']}, component: () => import("@/components/modules/Demo.vue")}
      ]
    },
    { path: "/404", component: () => import("@/components/modules/404.vue") },
]

/**
 * 创建路由
 */
const router = createRouter({
    history: createWebHashHistory(),
    routes: constantRoutes,
});

router.beforeEach((to, from, next) => {
    const routeExists = router.getRoutes().some(route => route.path === to.path);
    if (routeExists) {
        if(to.meta.auth){
            const token = Storage.get(import.meta.env.VITE_STORE_KEY)
            if(!token){
                next({ path: '/login' }); // 认证页面没有token 退回登录页
            }
        }
        next(); // 如果路由存在，继续正常的导航
    } else {
        next({ path: '/404' }); // 如果路由不存在，重定向到 NotFound 路由
    }
});

export default router;